<template>
  <div class="app-container">
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
      class="query-form"
    >
      <el-form-item label="日期" prop="gdate">
        <el-date-picker
          v-model="queryParams.gdate"
          clearable
          size="small"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="时间" prop="gtime">
        <!-- <el-date-picker
          v-model="queryParams.gtime"
          clearable
          size="small"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择时间"
        /> -->
        <el-time-picker
          v-model="queryParams.gtime"
          placeholder="任意时间点"
        />
      </el-form-item>
      <el-form-item label="桩号" prop="gloc">
        <el-input
          v-model="queryParams.gloc"
          placeholder="请输入桩号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="描述" prop="gincident">
        <el-input
          v-model="queryParams.gincident"
          placeholder="请输入描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="状态" prop="gstate">
        <el-input
          v-model="queryParams.gstate"
          placeholder="请输入状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fj:test:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fj:test:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fj:test:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fj:test:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row> -->
    <div
      class="content-container height_100-query_from"
    >
      <div class="handle_bar">
        <div class="tit"><span>机器人列表</span></div>
        <el-button
          type="primary"
          plain
          size="small"
          @click="handleAdd"
        >新增</el-button>
      </div>
      <div class="flex-1" style="position: relative;overflow: hidden;">
        <div style="position: absolute;left: 0;top: 0;bottom: 0;right: 0;display: flex;flex-direction: column;overflow: hidden;">
          <el-table
            v-loading="loading"
            class="flex-1"
            height="250"
            style="width:100%;overflow: hidden;"
            :data="testList"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="ID" align="center" prop="id" />
            <el-table-column label="日期" align="center" prop="gdate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.gdate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="时间" align="center" prop="gtime" width="180">
              <!-- <template slot-scope="scope">
            <span>{{ parseTime(scope.row.gtime, '{y}-{m}-{d}') }}</span>
          </template> -->
            </el-table-column>
            <el-table-column label="桩号" align="center" prop="gloc" />
            <el-table-column label="描述" align="center" prop="gincident" />
            <el-table-column label="状态" align="center" prop="gstate">
              <template slot-scope="scope">
                <span v-if="scope.row.gstate == 0" class="text-warning">未处理</span>
                <span v-else class="text-info">已处理</span>
              </template>
            </el-table-column>
            <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
             <el-button
              v-hasPermi="['fj:test:edit']"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button> -->
            <!-- <el-button
              v-hasPermi="['fj:test:remove']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>-->
          </el-table>
        </div>
      </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改机器人控制对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日期" prop="gdate">
          <el-date-picker
            v-model="form.gdate"
            clearable
            size="small"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="时间" prop="gtime">
          <!-- <el-date-picker
            v-model="form.gtime"
            clearable
            size="small"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择时间"
          /> -->
          <el-time-picker
            v-model="form.gtime"
            placeholder="任意时间点"
            value-format="hh:mm:ss"
          />
        </el-form-item>
        <el-form-item label="桩号" prop="gloc">
          <el-input v-model="form.gloc" placeholder="请输入桩号" />
        </el-form-item>
        <el-form-item label="描述" prop="gincident">
          <el-input v-model="form.gincident" type="textarea" placeholder="请输入描述" />
        </el-form-item>
        <!-- <el-form-item label="状态" prop="gstate">
          <el-input v-model="form.gstate" placeholder="请输入状态" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTest, getTest, delTest, addTest, updateTest } from '@/api/fjgs/robot'

export default {
  name: 'Test',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机器人控制表格数据
      testList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gdate: null,
        gtime: null,
        gloc: null,
        gincident: null,
        gstate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gdate: [{ required: true, message: '选择日期', trigger: 'blur' }],
        gtime: [{ required: true, message: '选择时间', trigger: 'blur' }],
        gloc: [{ required: true, message: '请输入桩号', trigger: 'blur' }],
        // gincident: [{ required: true, message: '选择目录', trigger: 'blur' }],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询机器人控制列表 */
    getList() {
      this.loading = true
      listTest(this.queryParams).then(response => {
        this.testList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gdate: null,
        gtime: null,
        gloc: null,
        gincident: null,
        gstate: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加机器人控制'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTest(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改机器人控制'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTest(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTest(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除机器人控制编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delTest(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fj/test/export', {
        ...this.queryParams
      }, `fj_test.xlsx`)
    }
  }
}
</script>
